<?php

class WoodleInstall {

	var $course_table;
	var $lecture_table;
	var $material_table;
	
	var $posts_table;

	function __construct() {
		global $wpdb;
		$this -> course_table = $wpdb -> prefix . "woo_course";
		$this -> lecture_table = $wpdb -> prefix . "woo_lecture";
		$this -> material_table = $wpdb -> prefix . "woo_material";
		
		$this -> posts_table = $wpdb -> prefix ."posts";
	}

	function activate() {
		$this -> woodle_create_page();
		$this -> woodle_install_db();
		// $this -> woodle_install_data();
	}

	function deactivate(){
		global $wpdb;	
		$page = get_page_by_title( 'Woodle' );
		wp_delete_post($page->ID);
	}
	
	// initialize tables
	function woodle_install_db() {
		require_once (ABSPATH . 'wp-admin/includes/upgrade.php');

		$sql_course = "CREATE TABLE " . $this -> course_table . " (
course_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
semester enum('SS','FS') NOT NULL,
year YEAR);";

		$sql_lecture = "CREATE TABLE " . $this -> lecture_table . " (
lecture_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
course_id INT NOT NULL,
name TEXT NOT NULL,
description TEXT,
date DATETIME NOT NULL,
FOREIGN KEY(course_id) REFERENCES " . $this -> course_table . "(course_id)
ON UPDATE CASCADE ON DELETE CASCADE);";

		$sql_material = "CREATE TABLE " . $this -> material_table . " (
material_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
lecture_id INT NOT NULL,
name TEXT NOT NULL,
url_id INT NOT NULL,
type enum('solution','assignment','article','tutorial','presentation') NOT NULL,
FOREIGN KEY(lecture_id) REFERENCES " . $this -> lecture_table . "(lecture_id)
ON UPDATE CASCADE ON DELETE CASCADE);";

		dbDelta($sql_course);
		dbDelta($sql_lecture);
		dbDelta($sql_material);
	}

	function woodle_install_data() {
		// global $wpdb;
 		// $wpdb->insert($this -> course_table, array('semester' => 'SS', 'name' => "WebEngineering", 'description' => "Test Description", 'year' => 2012));
		// $course_id = $wpdb->insert_id;
		// $wpdb->insert($this->lecture_table, array('course_id'=>$course_id, 'name'=>"First Lecture", 'description'=>"First description",'date'=>current_time('mysql')));
		// $lecture_id = $wpdb->insert_id;
		// // $wpdb->insert($this->material_table, array('lecture_id'=>$lecture_id, 'name'=>"First Material", 'url_id'=>"http://jontambek.files.wordpress.com/2011/11/tumblr_l5m1vrd9ol1qbn5jpo1_500.jpg", 'type'=>'assignment'));
		// // $wpdb->insert($this->material_table, array('lecture_id'=>$lecture_id, 'name'=>"Second Material", 'url_id'=>"http://jontambek.files.wordpress.com/2011/11/tumblr_l5m1vrd9ol1qbn5jpo1_500.jpg",'type'=>'assignment'));
		// $wpdb->insert($this->lecture_table, array('course_id'=>$course_id, 'name'=>"Second Lecture", 'description'=>"Second description",'date'=>current_time('mysql')));	
		// $lecture_id = $wpdb->insert_id;
		// // $wpdb->insert($this->material_table, array('lecture_id'=>$lecture_id, 'name'=>"First Material", 'url_id'=>"http://jontambek.files.wordpress.com/2011/11/tumblr_l5m1vrd9ol1qbn5jpo1_500.jpg", 'type'=>'assignment'));
		// // $wpdb->insert($this->material_table, array('lecture_id'=>$lecture_id, 'name'=>"Second Material", 'url_id'=>"http://jontambek.files.wordpress.com/2011/11/tumblr_l5m1vrd9ol1qbn5jpo1_500.jpg",'type'=>'assignment'));
	}

	function woodle_create_page() {
		// Create woodle page
		$my_post = array(
			'post_title' => 'Woodle', 
			'post_content' => '[woo]', 
			'post_status' => 'publish', 
			'comment_status' => 'closed', 
			'post_author' => 1, 
			'post_parent' => 0,
			'post_type' => 'page');
		$id = wp_insert_post($my_post);
	}	
}
?>